//
//  JKRFeedAuthorBottomView.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/10.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedAuthorBottomView.h"

@interface JKRFeedAuthorBottomView ()

@property (nonatomic, strong) YYLabel *statusLabel;

@property (nonatomic, strong) UIImageView *statusBottomImageView;

@property (nonatomic, strong) YYLabel *statusBottomLabel;

/// 聊一聊按钮
@property (nonatomic, strong) UIButton *chatButton;
/// 已经购买按钮
@property (nonatomic, strong) UIButton *hasBuyButon;
/// 立即购买按钮
@property (nonatomic, strong) UIButton *buyButton;
/// 确认服务按钮
@property (nonatomic, strong) UIButton *startButton;
/// 结束服务按钮
@property (nonatomic, strong) UIButton *finishButton;
/// 立即报名按钮
@property (nonatomic, strong) UIButton *signUpButton;
/// 取消报名按钮
@property (nonatomic, strong) UIButton *cancelSignUpButton;
/// 已经接单按钮
@property (nonatomic, strong) UIButton *hasSignUpButton;

@property (nonatomic, strong) UIView *bottomLineView;


@end

@implementation JKRFeedAuthorBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    @weakify(self);
    self.backgroundColor = [UIColor whiteColor];
    _statusLabel = [self _creatYYLabel];
    [self addSubview:_statusLabel];
    _statusBottomLabel = [self _creatYYLabel];
    _statusBottomLabel.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    _statusBottomLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weak_self highLightClickWithText:text range:range];
    };
    [self addSubview:_statusBottomLabel];
    _statusBottomImageView = [UIImageView new];
    _statusBottomImageView.userInteractionEnabled = YES;
    [_statusBottomImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImageViewClick)]];
    [self addSubview:_statusBottomImageView];
    _bottomLineView = [UIView new];
    _bottomLineView.backgroundColor = UIColorHex(E1E1E1);
    [self addSubview:_bottomLineView];
    
    _chatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_chatButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_chatButton setBackgroundColor:UIColorHex(F4AD3A)];
    [_chatButton setTitle:@"聊一聊" forState:UIControlStateNormal];
    _chatButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _chatButton.layer.cornerRadius = 3.f;
    [self addSubview:_chatButton];
    
    _hasBuyButon = [UIButton buttonWithType:UIButtonTypeCustom];
    [_hasBuyButon setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_hasBuyButon setBackgroundColor:UIColorHex(D8D8D8)];
    [_hasBuyButon setTitle:@"已经购买" forState:UIControlStateNormal];
    _hasBuyButon.titleLabel.font = [UIFont systemFontOfSize:14];
    _hasBuyButon.layer.cornerRadius = 3.f;
    [self addSubview:_hasBuyButon];
    
    _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_buyButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_buyButton setBackgroundColor:UIColorHex(CE0008)];
    [_buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
    _buyButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _buyButton.layer.cornerRadius = 3.f;
    [self addSubview:_buyButton];
    
    _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_startButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_startButton setBackgroundColor:UIColorHex(CE0008)];
    [_startButton setTitle:@"确认服务" forState:UIControlStateNormal];
    _startButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _startButton.layer.cornerRadius = 3.f;
    [self addSubview:_startButton];
    
    _finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_finishButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_finishButton setBackgroundColor:UIColorHex(CE0008)];
    [_finishButton setTitle:@"结束服务" forState:UIControlStateNormal];
    _finishButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _finishButton.layer.cornerRadius = 3.f;
    [self addSubview:_finishButton];
    
    _signUpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_signUpButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_signUpButton setBackgroundColor:UIColorHex(CE0008)];
    [_signUpButton setTitle:@"立即报名" forState:UIControlStateNormal];
    _signUpButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _signUpButton.layer.cornerRadius = 3.f;
    [self addSubview:_signUpButton];
    
    _cancelSignUpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelSignUpButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_cancelSignUpButton setBackgroundColor:UIColorHex(CE0008)];
    [_cancelSignUpButton setTitle:@"取消报名" forState:UIControlStateNormal];
    _cancelSignUpButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _cancelSignUpButton.layer.cornerRadius = 3.f;
    [self addSubview:_cancelSignUpButton];
    
    _hasSignUpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_hasSignUpButton setTitleColor:UIColorHex(FFFFFF) forState:UIControlStateNormal];
    [_hasSignUpButton setBackgroundColor:UIColorHex(D8D8D8)];
    [_hasSignUpButton setTitle:@"已经接单" forState:UIControlStateNormal];
    _hasSignUpButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _hasSignUpButton.layer.cornerRadius = 3.f;
    [self addSubview:_hasSignUpButton];
    
    return self;
}

- (void)setLayout:(JKRFeedBottomLayout *)layout {
    _layout = layout;
    self.hidden = !layout.show;
    if (!layout.show) {
        return;
    }
    @weakify(self);
    self.frame = layout.bottomFrame;
    self.statusLabel.frame = layout.statusLabelFrame;
    self.statusLabel.textLayout = layout.statusLabelLayout;
    self.statusBottomImageView.frame = layout.statusBottomImageViewFrame;
    [self.statusBottomImageView setImageWithURL:layout.statusBottomImageViewURL placeholder:nil options:kNilOptions manager:nil progress:nil transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        weak_self.statusBottomImageView.image = [image jkr_clipCircleImageWithBorder:0.5 withColor:UIColorHex(CCCCCC)];
    }];
    self.statusBottomLabel.frame = layout.statusBottomLabelFrame;
    self.statusBottomLabel.textLayout = layout.statusBottomLabelLayout;
    
    self.chatButton.frame = layout.chatButtonFrame;
    self.hasBuyButon.frame = layout.hasBuyButtonFrame;
    self.buyButton.frame = layout.buyButtonFrame;
    self.startButton.frame = layout.startButtonFrame;
    self.finishButton.frame = layout.finishButtonFrame;
    self.signUpButton.frame = layout.signUpButtonFrame;
    self.cancelSignUpButton.frame = layout.cancelSignUpButtonFrame;
    self.hasSignUpButton.frame = layout.hasSignUpButtonFrame;
    
    self.bottomLineView.frame = layout.bottomLineViewFrame;
}
                    
- (YYLabel *)_creatYYLabel {
    YYLabel *label = [YYLabel new];
    label.ignoreCommonProperties = YES;
    label.fadeOnAsynchronouslyDisplay = YES;
    return label;
}

- (void)highLightClickWithText:(NSAttributedString *)text range:(NSRange)range {
    YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:range.location];
    NSDictionary *info = highlight.userInfo;
    if (info.count == 0) {
        return;
    }
    if (info[kFeedTopClickUser]) {
        [self.topView.delegate topViewDidClickUserWithUserID:info[kFeedTopClickUser]];
        return;
    }
}

- (void)headerImageViewClick {
    YYTextHighlight *highlight = [self.statusBottomLabel.textLayout.text attribute:YYTextHighlightAttributeName atIndex:0];
    NSDictionary *info = highlight.userInfo;
    if (info && info.count && info[kFeedTopClickUser]) {
        [self.topView.delegate topViewDidClickUserWithUserID:info[kFeedTopClickUser]];
    }
}

@end
